<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Org\Image;

class UserController extends Controller
{
    /**
   *    后台首页页面
   */
   public function getAdd()
   {
        return view('/admin/user/add');
   }

   /**
   *    后台添加数据
   */
   public function postInsert(Request $request)
   {

//       $this->validate($request, [
//            'username' => 'required',
//            'password' => 'required',
//            'repassword' => 'required | same:password',
//            'email' => 'required | email:email',
//        ],[
//            'username.required' => '用戶名不能為空',
//            'password.required' => '密碼不能為空',
//            'repassword.required' => '確認密碼不能為空',
//            'repassword.same' => '兩次密碼不一致',
//            'email.required' => '邮箱不能为空',
//            'email.email' => '邮箱格式不正确',
//        ]);
//
//       $restult = DB::table('project_user')->where('username',$request->input('username'))->first();
//
//       if(!empty($restult)) {
//
//          return back()->with('error','用户名重复');
//
//          exit;
//       }
//
//       $arr = $request->only('username','password');

       if($request->hasFile('picname')) {

        //随机号
        $name = rand(10000,99999).time();
        //后缀名
        $suffix = $request->file('picname')->getClientOriginalExtension();

           dump($suffix);die;
        //图片文件
        $filename = $name.'.'.$suffix;   //执行图片压缩文件

        //存入本地
        $request->file('picname')->move('./upload/',$filename);

        //new对象
        $img = new Image;
        //调用方法
        $img->open('./upload/'.$filename)->thumb(100,100)->save('./upload/s_'.$filename);

       //写入数据库
       $arr['picname'] = "/upload/s_".$filename;

       //删除大图
        @unlink('./upload/'.$filename);

       }

       //用户详情表
       $details = $request->only('sex','email');

       $arr['password'] = Hash::make($arr['password']);

       $arr['token'] = str_random(30);

       //添加时间
       $arr['addtime'] = time();
 
       //默认昵称和用户名一样
      $arr['nickname'] = $arr['username'];

      $res = DB::table('project_user')->insertGetId($arr);

      $details['uid'] =  $res;
    
      $details['gold'] = 30;  //初次登陆赠30金币
      //注册 默认和最后登陆时间一直
      $details['regtime'] = $details['lasttime'] = time();
      //注册ip
      $details['regip'] = $request->ip();

      $result = DB::table('project_user_details')->insert($details);

        if($result && $res ) {

            return redirect('/Admin/user/index')->with('success','添加成功');
       
          } else {

            return back()->with('error','添加失败');
          }
        
   }

   /**
   *    修改用户页面
   */
   public function getEdit(Request $request)
   {
        $id = $request->input('id');
        //单条查询
        $res = DB::table('project_user')
         ->join('project_user_details','project_user.id','=','project_user_details.uid')
         ->select('project_user.username','project_user.nickname','project_user.id','project_user.level','project_user.status','project_user.picname','project_user_details.gold','project_user_details.sex','project_user.email')
         ->where('project_user.id',$id)
         ->first();
     
      //解析模板
      return view('/admin/user/edit',['res'=>$res]);
   }

   /**
   *    修改方法
   */
   public function postUpdate(Request $request)
   {

     $this->validate($request, [
            'username' => 'required',
            'email' => 'required | email:email',
        ],[
            'username.required' => '用戶名不能為空',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
        ]);

        $id = $request->only('id');
       
        $arr = $request->only('nickname','status','level','email');

        $details = $request->only('sex','gold','email');

       if($request->hasFile('picname')) {

        //随机号
        $name = rand(10000,99999).time();
        //后缀名
        $suffix = $request->file('picname')->getClientOriginalExtension();
       
        $filename = $name.'.'.$suffix;  //执行压缩的文件图片

        //写入本地
        $request->file('picname')->move('./upload/',$filename);

        //实例化对象
       $img = new Image;

       // 调用方法
       $img->open('./upload/'.$filename)->thumb(100,100)->save('./upload/s_'.$filename);

       //写入数据库
       $arr['picname'] = "/upload/s_".$filename;

        //获取就图片
        $row = DB::table('project_user')->where('id',$id)->first()->picname;

        @unlink('.'.$row);
        @unlink('./upload/'.$filename);
     
       }
    
      $res = DB::table('project_user')->where('id',$id)->update($arr);
     
      $restult = DB::table('project_user_details')->where('uid',$id)->update($details);
     
      if($res || $restult) {

        return redirect('/Admin/user/index')->with('success','修改成功');
      } else {

        return back()->with('error','修改失败');
      }

   }

   /**
   *    删除方法
   */
   public function getDelete(Request $request)
   {
      $id = $request->input('id');

      $res = DB::table('project_user')->where('id',$id)->delete();

      $restult = DB::table('project_user_details')->where('uid',$id)->delete();

      if($res && $restult) {

        return redirect('/Admin/user/index')->with('success','删除成功');
      } else {

        return back()->with('error','删除失败');
      }
   }


   /**
   *    后台显示数据
   */
   public function index(Request $request)
   {    
    //搜索 
    //条数  
       //  $arr = $request->all();
       // var_dump($arr);
        //查询
        $res = DB::table('project_user')
        ->join('project_user_details','project_user.id','=','project_user_details.uid')
        ->select('project_user.*','project_user_details.sex','project_user_details.email','project_user_details.gold','project_user_details.regip')
        ->where('username','like','%'.$request->input('search').'%')
        ->paginate($request->input('num',5));

       return view('/admin/user/index',['row'=>$res,'request'=>$request->all()]);
   }
}
